package Week2.J0426;

import java.util.Scanner;

public class 买卖股票的最好时机二_xv {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] price = new int[n];
        int sum = 0;

        for (int i = 0; i < price.length; i++) {
            price[i] = scanner.nextInt();
        }
        for (int i = 1; i < price.length; i++) {
            if (price[i-1] < price[i]) {
                sum += price[i]-price[i-1];
            }
        }
        System.out.println(sum);
    }
}
